<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>跨浏览器事件</title>
</head>

<body>
    <input type="button" value="点击" id="btn">
    <script>
        var eventUtil = {
            addHandler: function (element, type, handler) {
                //绑定事件
                //Chrome，firefox,ie9 addEventListener
                //IE8及以下的浏览器 attachEvent
                if (element.addEventListener) {
                    element.addEventListener(type, handler, false);
                } else if (element.attachEvent) {
                    element.attachEvent("on" + type, handler)
                } else {
                    element["on" + type] = null;
                }
            },
            removeHandler: function (element, type, handler) {
                if (element.removeEventListener) {
                    element.removeEventListener(type, handler, false);
                } else if (element.detachEvent) {
                    element.detachEvent("on" + type, handler)
                } else {
                    element["on" + type] = null;
                }
            },
            getTarget: function (event) {
                return event.target || event.srcElement;
            },
            preventDefault: function (event) {
                if (event.preventDefault) {
                    event.preventDefault();
                } else {
                    event.returnValue = false;
                }
            },
            stopPropagation: function (event) {
                if (event.stopPropagation) {
                    event.stopPropagation();
                } else {
                    event.cancelBubble = ture;
                }
            }
        }
        var btn = document.getElementById("btn");
        var handler = function () {
            alert("clicked");
        }
        eventUtil.addHandler(btn, "click", handler);
        eventUtil.removeHandler(btn, "click", handler);
    </script>
</body>

</html>